/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.gui.languagetable;

import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import langman.business.data.TranslationService;
import langman.business.data.model.Language;
import langman.util.doping.Bean;
import langman.util.doping.Inject;
import langman.util.guifactory.AbstractFXMLBeanControl;

@Bean
public class LanguageTable extends AbstractFXMLBeanControl {

    private final TranslationService translationService;

    @FXML
    private TableView<Language> languageTable;

    @FXML
    private TableColumn<Language, String> nameColumn;

    @FXML
    private TableColumn<Language, String> fileNameColumn;

    @Inject
    public LanguageTable(TranslationService translationService) {
        this.translationService = translationService;
    }

    @Override
    public void initializeAfterInjection() {
        configureGUI();
        languageTable.setItems(translationService.getLanguagesUnmodifiable());
    }

    private void configureGUI() {
        nameColumn.setCellValueFactory(stringCellDataFeatures -> stringCellDataFeatures.getValue().nameProperty());
        nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        nameColumn.setEditable(false);
        fileNameColumn.setCellValueFactory(cellDataFeatures -> cellDataFeatures.getValue().fileNameProperty());
        fileNameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        fileNameColumn.setEditable(false);
        languageTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        languageTable.setEditable(false);
    }
}
